package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.UserLocation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import javax.annotation.Resource;
import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi{

    @Resource
    private MongoTemplate mongoTemplate;


    /**
     * 上传地理位置
     * @param latitude
     * @param longitude
     * @param addrStr
     * @param userId
     * @return
     */
    @Override
    public boolean location(double latitude, double longitude, String addrStr, Long userId) {
        try {
            //查询是否存在
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            if (location == null){
                //不存在，则为新增操作
                location.setUserId(userId);
                location.setLocation(new GeoJsonPoint(longitude,latitude));
                location.setAddress(addrStr);
                location.setCreated(System.currentTimeMillis());
                location.setUpdated(System.currentTimeMillis());
                location.setLastUpdated(System.currentTimeMillis());
                mongoTemplate.insert(location);
            }else {
                //存在，则为修改操作
                Update update = Update.update("location",new GeoJsonPoint(longitude,latitude))
                        .set("address",addrStr)
                        .set("updated",System.currentTimeMillis())
                        .set("lastUpdated",location.getUpdated());
                mongoTemplate.updateFirst(query,update,UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 搜索附近用户
     * @param userId
     * @return
     */
    @Override
    public List<Long> search(Long userId, double distance) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if (location == null){
            return null;
        }
        //确定圆心
        GeoJsonPoint point = location.getLocation();
        //确定半径
        Distance r = new Distance(distance / 1000, Metrics.KILOMETERS);
        //确定圆
        Circle circle = new Circle(point,r);
        //查询
        Query locationQuery = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> list = mongoTemplate.find(locationQuery, UserLocation.class);
        return CollUtil.getFieldValues(list,"userId",Long.class);
    }


}
